package com.my.mall.client.wechat.controller;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.my.mall.client.wechat.dto.UserDetailsClientDTO;
import com.my.mall.client.wechat.dto.UserInfoClientDTO;
import com.my.mall.client.wechat.service.WXClientService;
import com.my.mall.client.wechat.vo.OrderInfoClientVo;
import com.my.mall.client.wechat.vo.ShippingAddressClientVo;
import com.my.mall.client.wechat.vo.UserDetailsClientVo;
import com.my.mall.client.wechat.vo.UserInfoClientVo;
import com.my.mall.common.HttpUtils;
import com.my.mall.common.R;

@RestController
@RequestMapping("client/wx")
public class WXClientController {
	@Autowired
	WXClientService wxClientService;

	/**
	 * 用户信息表的数据添加
	 * 
	 * @param jsCode
	 * @return
	 */
	@RequestMapping("getOpenId/{jsCode}")
	public R getOpenId(@PathVariable("jsCode") String jsCode) {
		String appid = "wxc01faef8b8897feb";
		String secret = "cbc5cbb44f93755cb18127922cc181db";
		String url = "https://api.weixin.qq.com/sns/jscode2session?appid=" + appid + "&secret=" + secret + "&js_code="
				+ jsCode + "&grant_type=authorization_code";
		String result = HttpUtils.httpGet(url);
		String openid = "";
		String sessionKey = "";
		if (result != null && !"".equals(result)) {
			openid = JSON.parseObject(result).getString("openid");
			sessionKey = JSON.parseObject(result).getString("session_key");
			if (openid != null) {
				Date currentTime = new Date();
				UserInfoClientDTO wxClientDTO = new UserInfoClientDTO();
				wxClientDTO.setOpenid(openid);
				wxClientDTO.setLastVisitTime(currentTime);
				wxClientDTO.setRegisterTime(currentTime);
				wxClientDTO.setSessionKey(sessionKey);
				wxClientDTO.setState(1);
				wxClientService.saveUserInfo(wxClientDTO);
				return R.ok(openid);
			} else {
				return R.error("用户唯一标识不存在！");
			}
		} else {
			return R.error("账号已过期！");
		}
	}

	/**
	 * 用户信息表的数据更新
	 * 
	 * @param wxClientDTO
	 * @return
	 */
	@RequestMapping("updateUserInfo")
	@ResponseBody
	public UserInfoClientVo updateUserInfo(@RequestBody UserInfoClientDTO wxClientDTO) {
		UserInfoClientVo wxClientVo = new UserInfoClientVo();
		wxClientService.updateUserInfo(wxClientDTO);
		wxClientVo = wxClientService.getUserByOpenId(wxClientDTO.getOpenid());
		return wxClientVo;
	}

	/**
	 * 用户信息详情表的数据添加或更新
	 * @param userDetailsClientDTO
	 * @return
	 */
	@RequestMapping("insertOrUpdateUserDetails")
	public R insertOrUpdateUserDetails(@RequestBody UserDetailsClientDTO userDetailsClientDTO) {
		if (userDetailsClientDTO.getOpenid() != null) {
			UserDetailsClientVo userDetails = wxClientService.getUserDetailsByOpenId(userDetailsClientDTO.getOpenid());
			if (userDetails != null) {
				wxClientService.updateUserDetails(userDetailsClientDTO);
			} else {
				wxClientService.saveUserDetails(userDetailsClientDTO);
			}
			return R.ok();
		}
		return R.error("用户唯一标识不存在！");

	}
	
	/**
	 * 查询该用户的所有订单
	 * @param userId
	 * @return
	 */
	@RequestMapping("selectOrderInfoList/{userId}")
	@ResponseBody
	public List<OrderInfoClientVo> selectOrderInfoList(@PathVariable("userId") int userId) {
		return wxClientService.selectOrderInfoList(userId);
	}
	
	/**
	 * 查询该用户的所有收获地址
	 * @param userId
	 * @return
	 */
	@RequestMapping("selectShippingAddressList/{userId}")
	@ResponseBody
	public List<ShippingAddressClientVo> selectShippingAddressList(@PathVariable("userId") int userId) {
		return wxClientService.selectShippingAddressList(userId);
	}

}
